package lib.quasar.widget.image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import lib.quasar.util.DimenUtil;
import lib.quasar.widget.R;

/**
 * description: 聊天头像 - 未读消息
 * created by kalu on 2018/7/14 8:51
 */
public final class ChatImageView extends android.support.v7.widget.AppCompatImageView {

    private final Paint mPaint = new Paint();
    private final RectF mBadgeRectF = new RectF();
    private final Rect mTextRect = new Rect();

    private int mBadgeNumber = 0;

    private int mBadgeBackgroundColor = Color.RED;
    private int mBadgeTextColor = Color.WHITE;
    private float mBadgeTextSize = 10f;


    public ChatImageView(Context context) {
        super(context);
    }

    public ChatImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = null;
        try {
            array = context.obtainStyledAttributes(attrs, R.styleable.ChatImageView);
            mBadgeBackgroundColor = array.getColor(R.styleable.ChatImageView_civ_badge_bg_color, mBadgeBackgroundColor);
            mBadgeTextColor = array.getColor(R.styleable.ChatImageView_civ_badge_text_color, mBadgeTextColor);
            mBadgeTextSize = array.getDimension(R.styleable.ChatImageView_civ_badge_text_size, mBadgeTextSize);
        } catch (Exception e) {
        } finally {
            if (null != array) {
                array.recycle();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBadgeNumber == 0)
            return;

        final float size = DimenUtil.dp2px(18f);

        mPaint.reset();
        mPaint.clearShadowLayer();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setColor(mBadgeBackgroundColor);
        mBadgeRectF.left = 0f;
        mBadgeRectF.top = 0f;
        mBadgeRectF.right = size;
        mBadgeRectF.bottom = size;
        canvas.drawOval(mBadgeRectF, mPaint);

        mPaint.reset();
        mPaint.clearShadowLayer();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setTextSize(mBadgeTextSize);
        mPaint.setColor(mBadgeTextColor);
        mPaint.setTextAlign(Paint.Align.CENTER);
        final String number = mBadgeNumber > 99 ? "99+" : String.valueOf(mBadgeNumber);
        mPaint.getTextBounds(number, 0, number.length(), mTextRect);
        canvas.drawText(number, size * 0.5f, size * 0.5f + mTextRect.height() * 0.35f, mPaint);
    }

    public void setBadgeNumber(int badgeNumber) {
        this.mBadgeNumber = badgeNumber;
        postInvalidate();
    }
}
